<?php

class Service_Model extends CI_Model{
    
    public function __construct() {
        parent:: __construct();
    }
    
    public function getUser($id = null){
        if ($id){
            $query = $this->db->get_where('user', array('id'=>$id));
            $result = $query->row_array();
        } else {
            $query = $this->db->get('user');
            $result = $query->result_array();
        }
        if ($result) {
            return $result;
        } else {
            return false;
        }
    }
    
    public function getUserLevel($userId = null){
        if ($userId){
            $this->db->select('*');
            $this->db->from('user_level');
            $this->db->join('level', 'user_level.level_id = level.id');
            $this->db->where('user_id',$userId);
            $query = $this->db->get();
            $result = $query->result_array();
        } else {
            // TODO: get many user.
//            $query = $this->db->get('user');
//            $result = $query->result_array();
			return false;
        }
        if ($result) {
            return $result;
        } else {
            // Insert new field in user_level table
            $this->db->query("INSERT INTO user_level (user_id, level_id) VALUES ($userId, 1);");
            
            // Get the data back
            $this->db->select('*');
            $this->db->from('user_level');
            $this->db->join('level', 'user_level.level_id = level.id');
            $this->db->where('user_id',$userId);
            $query = $this->db->get();
            $result = $query->result_array();
            
            return $result;
        }
    }
    
    public function getQuizz($id=null, $random=null){
        if ($id){
            $query = $this->db->get_where('quizz', array('quizz_id'=>$id));
            $result = $query->row_array();
        } else if ($random) {
        	$this->db->where('approve',1);
            $this->db->order_by('quizz_id', 'RANDOM');
            $this->db->limit($random);
            $query = $this->db->get('quizz');
            $result = $query->result_array();
        } else {
            $query = $this->db->get('quizz');
            $result = $query->result_array();
        }
        if ($result) {
            return $result;
        } else {
            return false;
        }
    }
    
    public function insertUserLevel($userId, $levelId, $score, $coin, $time, $star){
        $data = array(
            'user_id'=>$userId,
            'level_id' => $levelId,
            'play_time' => $time,
            'star' => $star
        );
        
        $query = $this->db->get_where('user_level', array('user_id'=>$userId, 'level_id'=>$levelId));
        
        if ($query->row_array()){
            // Existed, Update.
            $this->updateUserLevel($userId, $levelId, $score, $coin, $time, $star);
            $result = true;
        } else {
            $this->db->insert('user_level', $data);
            $result = $this->db->insert_id();
        }
        
        
        if ($result){
            return $result;
        } else {
            return FALSE;
        }
    }
    
    public function updateUserLevel($userId, $levelId, $score, $coin, $time, $star){
        // Update to user_level table and user.
        
        // Update user table
        $dataUser = array(
            'score' => $score,
            'coin' => $coin
        );
        $this->db->where('id', $userId);
        $this->db->update('user', $dataUser);
        
        // Update or insert user_level
        $query = $this->db->get_where('user_level', array('user_id'=>$userId, 'level_id'=>$levelId));
        $dataLevel = array(
            'play_time' => $time,
            'star' => $star
        );
        if ($query->row_array()){
            $this->db->where('user_id', $userId);
            $this->db->where('level_id', $levelId);
            return $this->db->update('user_level', $dataLevel);
        } else {
            return $this->createUserLevel($userId, $levelId, $time, $star);
        }
    }
    
    public function createUserLevel($userId, $levelId, $time, $star){
        $data = array(
            'user_id' => $userId,
            'level_id' => $levelId,
            'play_time' => $time,
            'star' => $star
        );
        return $this->db->insert('user_level', $data);      
    }
    
    public function getLevel($id = null){
        if ($id){
            $query = $this->db->get_where('level', array('id'=>$id));
            $result = $query->row_array();
        } else {
            $query = $this->db->get('level');
            $result = $query->result_array();
        }
        if ($result) {
            return $result;
        } else {
            return false;
        }
    }
    
    public function deleteLevel($id){
        try {
            $this->db->delete('level', array('id'=>$id));
            if ($this->db->affected_rows() == 0){
                return FALSE;
            }
            return TRUE;
        } catch (Exception $e){
            return "error";
        }
    }
}
